#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <limits.h>
#include <math.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <errno.h>
#include <sys/fcntl.h>
#include <poll.h>
#include "plug_gpio.h"
#include "button_control.h"
#include "atchannel.h"
#include "at_tok.h"

static HI_BOOL g_bDetectStart = HI_FALSE;

static HI_VOID * LTE_DetectKeyThread (HI_VOID *param)
{
    HI_S32 s32Ret;
    HI_CHAR buff[10] = {0};
    HI_CHAR azATPhoneCall[128] = {0};
    HI_CHAR azPhone_number[20] = "8615002993697";
    HI_CHAR azTextMsg[64] = "jiayitext\x1a";
    HI_CHAR *cmd1 = HI_NULL;
    HI_CHAR *cmd2 = HI_NULL;
    HI_CHAR * response_line = HI_NULL;
    HI_U32  sendLength = 26;
    ATResponse *p_response = HI_NULL;
    struct pollfd fds1[1];
    HI_BOOL bStart = *(HI_BOOL *)param;
    int gpio1_fd = -1;
    gpio1_fd = open("/sys/class/gpio/gpio1/value", O_RDONLY);
    if (gpio1_fd < 0){
    	printf("Failed to open gpio1 !\n");
    }
    fds1[0].fd     = gpio1_fd;
    fds1[0].events = POLLPRI;
    while(bStart)
    {
        s32Ret = read(gpio1_fd, buff, 10);
        if (s32Ret == -1){
        	printf("gpio1 read error\n");
        }

        s32Ret = poll(fds1, 1, 0);
        if (s32Ret == -1){
            printf("gpio1 poll error\n");
        }

        if (fds1[0].revents & POLLPRI){
            s32Ret = lseek(gpio1_fd, 0, SEEK_SET);
            if (s32Ret == -1){
                printf("gpio1 lseek error\n");
            }
            fflush(stdout);
            printf("sw2 Pressed \n");
            /*按键触发发送*/
            system("ifconfig");
            //system("/mnt/sendSms.sh 15002993697  ""  "你好你好了吗gigigigi"");
            
//            s32Ret = at_send_command("atd15002993697;\r", &p_response);
//            if (0 > s32Ret || (p_response != NULL && 0 == p_response->success))
//            {  
//                s32Ret = HI_FAILURE;
//                printf("===================  failure!!!! ==========================\n");
//            }
//            else
//            {
//                printf("=================== ATPhoneCall  success!!!! ==========================\n");
//            }
//                s32Ret = at_send_command("AT+CMGF=0", &p_response);
//                usleep(2000*1000);
//                if (0 > s32Ret || (p_response != NULL && 0 == p_response->success))
//                {  
//                	s32Ret = HI_FAILURE;
//                	printf(" AT+CMGF=0  failure!!!!\n");
//                }
//                else
//                {
//                    printf("======= AT+CMGF=0  success!!!! ============\n");
//                }
//                fflush(stdin);
//                p_response = HI_NULL;
//                asprintf(&cmd1, "AT+CMGS=%d", sendLength);
//                asprintf(&cmd2, "0891683108702505F001000D91685100923996F7001B0C54C854C854C854C854C854C8");
//
//                s32Ret = at_send_command_sms(cmd1, cmd2, "+CMGS:", &p_response);
//                if (0 != s32Ret || (p_response != NULL && 0 == p_response->success))
//                {  
//                    printf(" AT+CMGF=0  failure!!!!\n");
//                }
//                else
//                {
//                    free(cmd1);
//                    free(cmd2);
//                    printf("======= AT+CMGF=0  success!!!! ============\n");
//                    printf("======= AT+CMGF=0  success!!!! ============\n");
//                }
                

            }	

            usleep(3000);
    }	

}


HI_S32 PLUG_DETECT_KEY_Init(HI_BOOL bStart)
{
    HI_S32 s32Ret;
    pthread_t threadid;
    g_bDetectStart = bStart;
    init_gpio1();
    s32Ret = pthread_create(&threadid, NULL, LTE_DetectKeyThread, (HI_BOOL *)&bStart);
    s32Ret |= pthread_detach(threadid);
    if (s32Ret < 0)
    {
        printf("pthread_create LTE_StartThread fail! %#x\n", s32Ret);
        return HI_FAILURE;
    }

}

